﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.ExceptionHandler;

public partial class Stock_RubrosAdmin : GenericPage
{
    List<Rubros> listaRubros = new List<Rubros>();    
    Rubros oRubro = new Rubros();

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                ListaRubros();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnBuscar_Click
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BMRubros bmRubros = new BMRubros(Connection);
            if (txtCodigo.Text.Trim() != "")
            {
                oRubro.Id_Rubro = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                oRubro.Id_Rubro = 0;
            }

            oRubro.Rubro = txtRubro.Text.Trim();

            listaRubros = bmRubros.BMRubrosSearch(oRubro);

            Session["listaRubros"] = listaRubros;
            CargarGrilla();

        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }
    #endregion

    #region gvRubros_PageIndexChanging
    protected void gvRubros_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvRubros.PageIndex = e.NewPageIndex;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvRubros_SelectedIndexChange
    protected void gvRubros_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Rubros rubro = new Rubros();
            rubro.Id_Rubro = (int)gvRubros.SelectedDataKey.Value;
            List<Rubros> listaRubros = (List<Rubros>)Session["listaRubros"];
            foreach (Rubros item in listaRubros)
            {
                if (item.Id_Rubro == rubro.Id_Rubro)
                {
                    rubro.Rubro = item.Rubro;
                    txtRubro2.Text = item.Rubro;
                    break;
                }
            }
            Session["Rubro"] = rubro;
            tblAdmin.Visible = false;
            tblGestion.Visible = true;
            lblAccion.Text = "Modificar Descripción de Nuevo Rubro";
            hdnAccion.Value = "Modifica";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnNuevo_Click
    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        try
        {
            tblAdmin.Visible = false;
            tblGestion.Visible = true;
            lblAccion.Text = "Ingresar Descripción de Nuevo Rubro";
            hdnAccion.Value = "Alta";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnGuardar_Click
    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtRubro2.Text != "")
            {
                if (hdnAccion.Value == "Alta")
                {
                    AltaRubro();
                }
                if (hdnAccion.Value == "Modifica")
                {
                    ModificaRubro();
                }
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

    #region Métodos

    #region CargarGrilla
    private void CargarGrilla()
    {
        try
        {
            listaRubros = (List<Rubros>)Session["listaRubros"];
            gvRubros.DataSource = listaRubros;
            gvRubros.DataBind();
            gvRubros.SelectedIndex = -1;
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region ListaRubros
    protected void ListaRubros()
    {
        try
        {
            BMRubros bmRubros = new BMRubros(Connection);
            oRubro = new Rubros();
            listaRubros = new List<Rubros>();
            List<Rubros> listaTemp = new List<Rubros>();
            listaTemp = bmRubros.BMRubrosSearch(oRubro);
            for (int i = 0; i < listaTemp.Count; i++)
            {
                listaRubros.Add(listaTemp[i]);
            }
            Session["listaRubros"] = listaRubros;
            CargarGrilla();
            tblGestion.Visible = false;
            hdnAccion.Value = "";
            txtCodigo.Text = "";
            txtRubro.Text = "";
            txtRubro2.Text = "";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region AltaRubro
    protected void AltaRubro()
    {
        try
        {
            BMRubros bmRubros = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            rubro.Rubro = txtRubro2.Text;
            bmRubros.BMRubrosInsert(rubro);
            tblAdmin.Visible = true;
            tblGestion.Visible = false;
            ListaRubros();
            lblAccion.Text = "Rubros Actualizados";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region ModificaRubro
    protected void ModificaRubro()
    {
        try
        {
            BMRubros bmRubros = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            rubro = (Rubros)Session["Rubro"];
            rubro.Rubro = txtRubro2.Text;
            bmRubros.BMRubrosUpdate(rubro);
            tblAdmin.Visible = true;
            tblGestion.Visible = false;
            ListaRubros();
            lblAccion.Text = "Rubros Actualizados";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

}
